<template>
  <Dialog
    :title="t('judgeScene.final.debateStudent')"
    v-model="dialogVisible"
    width="1400"
    :fullscreen="false"
  >
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item>
          <SendChat2
            :list="chatList"
            class="mr-5px !ml-0"
            :title="t('judgeScene.final.noticeStudent')"
            :default-message="`你申请的[${awards?.awardsName ?? '奖项'}]答辩时间已确定，请尽快登录系统查看。`"
          />
          <el-button type="success" plain @click="handleExport" :loading="exportLoading">
            <Icon icon="ep:download" class="mr-5px" />
            {{ t('action.export') }}
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <vxe-table
      ref="tableRef"
      id="iScxDebateStudent-table"
      v-loading="loading"
      :data="list"
      stripe
      :row-config="{ keyField: 'id' }"
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
      :checkbox-config="{ reserve: true }"
      @checkbox-all="collectSelect"
      @checkbox-change="collectSelect"
    >
      <vxe-column type="checkbox" width="50" align="center" />
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('scxFinalCandidate.fields.studentCode')"
        field="studentCode"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalCandidate.fields.studentName')"
        field="studentName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalCandidate.fields.deptName')"
        field="deptName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalCandidate.fields.major')"
        field="major"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalCandidate.fields.primaryMentor')"
        field="primaryMentor"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalCandidate.fields.debateTime')"
        field="debateTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="150"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleEditDebateTime(scope.row)">
            {{ t('scxFinalCandidate.form.title') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <ScxFinalCandidateForm ref="formRef" @success="getList" />
  </Dialog>
</template>

<script setup lang="ts">
import { vxeDateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ScxFinalCandidateApi, ScxFinalCandidateDebateVO } from '@/api/awards/scxfinalcandidate'
import type { VxeTableInstance, VxeTablePropTypes } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import SendChat2 from '@/components/SendChat2/index.vue'
import { AccountSelectVO } from '@/api/system/account'
import ScxFinalCandidateForm from '@/views/awards/scxfinalcandidate/ScxFinalCandidateForm.vue'
import { InstanceApi, InstanceVO } from '@/api/awards/instance'

/** 师昌绪答辩学生 列表 */
defineOptions({ name: 'ScxDebateStudentList' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false)

const loading = ref(true) // 列表的加载中
const list = ref<ScxFinalCandidateDebateVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  awardsId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

const chatList = ref<AccountSelectVO[]>([])
const tableRef = ref<VxeTableInstance>()
const collectSelect = () => {
  chatList.value = [
    ...(tableRef.value?.getCheckboxRecords() ?? []),
    ...(tableRef.value?.getCheckboxReserveRecords() ?? [])
  ].map((item: ScxFinalCandidateDebateVO) => {
    return {
      id: item.accountId,
      studentName: item.studentName,
      studentCode: item.studentCode,
      phone: item.phone,
      email: item.email
    } as AccountSelectVO
  })
}

const reset = () => {
  queryParams.pageNo = 1
  queryParams.pageSize = 10
  list.value = []
  total.value = 0
}

const open = (awardsId: string) => {
  dialogVisible.value = true
  reset()
  queryParams.awardsId = awardsId as any
  getList()
  getAwards(awardsId)
}

const awards = ref<InstanceVO>()
const getAwards = async (awardsId: string) => {
  awards.value = await InstanceApi.getInstance(awardsId)
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ScxFinalCandidateApi.getScxFinalCandidatePageByAwards(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ScxFinalCandidateApi.exportScxFinalCandidate(queryParams)
    download.excel(data, '师昌绪答辩学生安排.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof ScxFinalCandidateForm>>()
const handleEditDebateTime = (row: ScxFinalCandidateDebateVO) => {
  formRef.value?.open(row.id)
}

defineExpose({ open })
</script>
